import '../utils'

/**
 * HTTP 请求通用工具包
 * @type {{}}
 */
const url = window.document.location.href.toString(); // url 地址分析
const domain = url.split('?')[0];
const uriReg = new RegExp('\\?.*#/');
const uriStr = empty(url.match(uriReg)) ? '' : url.match(uriReg)[0].replace(/(^\?)|(#\/$)/g, "");
const uri = empty(uriStr) ? [] : uriStr.split("&");
const get = {};
for (let i in uri) {
  let j = uri[i].split("=");
  get[j[0]] = j[1];
}

let http = {
  url: url,
  domain: domain,
  params: get
};

http.init = (callback) => {
  if (typeof callback === 'function') {
    callback();
  }
};

http.urlBuilder = (key, value) => {
  let url = http.domain + "?";
  if (!empty(key)) {
    http.params[key] = value;
  }
  for (let i in http.params) {
    url = url + i + "=" + http.params[i] + "&";
  }
  url = url.substring(0, url.length - 1);
  history.replaceState(200, "", url);
};

export default http;
